/*******************************************************************************
 * Copyright (c) 2011 EclipseDevX.org and others.
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at:
 * 
 *     http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipsedevx.appengine.usage;

import java.io.PrintWriter;
import java.util.Stack;

/**
 * 
 */
public class HtmlWriter {
  private PrintWriter writer;

  private Stack<String> tags = new Stack<String>();
  
  public HtmlWriter(String pageTitle, PrintWriter writer) {
    this(pageTitle, writer, "");
  }
  
  public HtmlWriter(String pageTitle, PrintWriter writer, String pathFromRoot) {
    this.writer = writer;

    startPage(pageTitle, pathFromRoot);
  }

  public void println(String str) {
    writer.println(str);
  }
  
  public void startTag(String tag) {
    writer.println("<" + tag + ">");
    
    tags.push(tag);
  }
  
  public void startTag(String tag, String attributes) {
    writer.println("<" + tag + " " + attributes + ">");
    
    tags.push(tag);
  }
  
  public void writeTag(String tag, String content) {
    writer.println("<" + tag + ">" + content + "</" + tag + ">");
  }
  
  public void endTag() {
    writer.println("</" + tags.pop() + ">");
  }
  
  public void writePageTitle(String title) {
    writer.println("<div id=\"ae-appbar-lrg\" class=\"g-section\">"); 
    writer.println("<h1>" + title + "</h1>"); 
    writer.println("</div>");  
  }
  
  public void close() {
    endPage();

    writer.close();
  }

  private void endPage() {
    writer.println("</div></body>");
    writer.println("</html>");
  }

  private void startPage(String pageTitle, String pathFromRoot) {
    writer.println("<html>");
    writer.println("<head>");
    writeTag("title", pageTitle);
    writer.println("<link rel=\"stylesheet\" href=\"" + pathFromRoot + "appstats.css\">");
    writer.println("</head>");
    writer.println("<body><div class=\"g-doc\">");
  }

  public void writeTableTitle(String title) {
    writeTableTitle(title, "");
  }
  
  public void writeTableTitle(String title, String details) {
    startTag("caption");
    
    startTag("div", "class=\"g-section g-tpl-50-50 g-split\"");
    
    writeTag("strong", title);
    
    startTag("div", "class=\"g-unit\"");
    println(details);
    endTag(); //div
    
    endTag(); //div
    
    endTag(); // caption
  }

}
